/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : stm32f1xx_hal_msp.c
  * Description        : This file provides code for the MSP Initialization
  *                      and de-Initialization codes.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */
extern DMA_HandleTypeDef hdma_adc1;

extern DMA_HandleTypeDef hdma_i2c1_rx;

extern DMA_HandleTypeDef hdma_i2c2_tx;

extern DMA_HandleTypeDef hdma_usart2_rx;

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */

/* USER CODE END TD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN Define */

/* USER CODE END Define */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN Macro */

/* USER CODE END Macro */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* External functions --------------------------------------------------------*/
/* USER CODE BEGIN ExternalFunctions */

/* USER CODE END ExternalFunctions */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
                    /**
  * Initializes the Global MSP.
  */
void HAL_MspInit(void)
{
  /* USER CODE BEGIN MspInit 0 */

  /* USER CODE END MspInit 0 */

  __HAL_RCC_AFIO_CLK_ENABLE();
  __HAL_RCC_PWR_CLK_ENABLE();

  /* System interrupt init*/

  /** NOJTAG: JTAG-DP Disabled and SW-DP Enabled
  */
  __HAL_AFIO_REMAP_SWJ_NOJTAG();

  /* USER CODE BEGIN MspInit 1 */

  /* USER CODE END MspInit 1 */
}

/**
* @brief ADC MSP Initialization
* This function configures the hardware resources used in this example
* @param hadc: ADC handle pointer
* @retval None
*/
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hadc->Instance==ADC1) {
	  /* USER CODE BEGIN ADC1_MspInit 0 */

	  /* USER CODE END ADC1_MspInit 0 */
	  /* Peripheral clock enable */
	  __HAL_RCC_ADC1_CLK_ENABLE();

	  __HAL_RCC_GPIOA_CLK_ENABLE();
	  /**ADC1 GPIO Configuration
	  PA0-WKUP     ------> ADC1_IN0
	  PA1     ------> ADC1_IN1
	  */
	  GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
	  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
	  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

	  /* ADC1 DMA Init */
	  /* ADC1 Init */
	  hdma_adc1.Instance = DMA1_Channel1;
	  hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
	  hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
	  hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
	  hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
	  hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
	  hdma_adc1.Init.Mode = DMA_NORMAL;
	  hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
	  if (HAL_DMA_Init(&hdma_adc1) != HAL_OK) {
		  Error_Handler();
	  }

	  __HAL_LINKDMA(hadc, DMA_Handle, hdma_adc1);

	  /* USER CODE BEGIN ADC1_MspInit 1 */

	  /* USER CODE END ADC1_MspInit 1 */
  }

}

/**
* @brief ADC MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hadc: ADC handle pointer
* @retval None
*/
void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
{
  if(hadc->Instance==ADC1) {
	  /* USER CODE BEGIN ADC1_MspDeInit 0 */

	  /* USER CODE END ADC1_MspDeInit 0 */
	  /* Peripheral clock disable */
	  __HAL_RCC_ADC1_CLK_DISABLE();

	  /**ADC1 GPIO Configuration
	  PA0-WKUP     ------> ADC1_IN0
	  PA1     ------> ADC1_IN1
	  */
	  HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0 | GPIO_PIN_1);

	  /* ADC1 DMA DeInit */
	  HAL_DMA_DeInit(hadc->DMA_Handle);
	  /* USER CODE BEGIN ADC1_MspDeInit 1 */

	  /* USER CODE END ADC1_MspDeInit 1 */
  }

}

/**
* @brief I2C MSP Initialization
* This function configures the hardware resources used in this example
* @param hi2c: I2C handle pointer
* @retval None
*/
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hi2c->Instance==I2C1)
  {
  /* USER CODE BEGIN I2C1_MspInit 0 */

  /* USER CODE END I2C1_MspInit 0 */

    __HAL_RCC_GPIOB_CLK_ENABLE();
	  /**I2C1 GPIO Configuration
	  PB8     ------> I2C1_SCL
	  PB9     ------> I2C1_SDA
	  */
	  GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
	  GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
	  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

	  __HAL_AFIO_REMAP_I2C1_ENABLE();

	  /* Peripheral clock enable */
	  __HAL_RCC_I2C1_CLK_ENABLE();

	  /* I2C1 DMA Init */
	  /* I2C1_RX Init */
	  hdma_i2c1_rx.Instance = DMA1_Channel7;
	  hdma_i2c1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
	  hdma_i2c1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
	  hdma_i2c1_rx.Init.MemInc = DMA_MINC_ENABLE;
	  hdma_i2c1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
	  hdma_i2c1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
	  hdma_i2c1_rx.Init.Mode = DMA_NORMAL;
	  hdma_i2c1_rx.Init.Priority = DMA_PRIORITY_LOW;
	  if (HAL_DMA_Init(&hdma_i2c1_rx) != HAL_OK) {
		  Error_Handler();
	  }

	  __HAL_LINKDMA(hi2c, hdmarx, hdma_i2c1_rx);

	  /* I2C1 interrupt Init */
	  HAL_NVIC_SetPriority(I2C1_EV_IRQn, 1, 0);
	  HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
	  /* USER CODE BEGIN I2C1_MspInit 1 */

	  /* USER CODE END I2C1_MspInit 1 */
  }
  else if(hi2c->Instance==I2C2)
  {
  /* USER CODE BEGIN I2C2_MspInit 0 */

  /* USER CODE END I2C2_MspInit 0 */

    __HAL_RCC_GPIOB_CLK_ENABLE();
	  /**I2C2 GPIO Configuration
	  PB10     ------> I2C2_SCL
	  PB11     ------> I2C2_SDA
	  */
	  GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11;
	  GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
	  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

	  /* Peripheral clock enable */
	  __HAL_RCC_I2C2_CLK_ENABLE();

	  /* I2C2 DMA Init */
	  /* I2C2_TX Init */
	  hdma_i2c2_tx.Instance = DMA1_Channel4;
	  hdma_i2c2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
	  hdma_i2c2_tx.Init.PeriphInc = DMA_PINC_DISABLE;
	  hdma_i2c2_tx.Init.MemInc = DMA_MINC_ENABLE;
	  hdma_i2c2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
	  hdma_i2c2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
	  hdma_i2c2_tx.Init.Mode = DMA_NORMAL;
	  hdma_i2c2_tx.Init.Priority = DMA_PRIORITY_LOW;
	  if (HAL_DMA_Init(&hdma_i2c2_tx) != HAL_OK) {
		  Error_Handler();
	  }

	  __HAL_LINKDMA(hi2c, hdmatx, hdma_i2c2_tx);

	  /* USER CODE BEGIN I2C2_MspInit 1 */

	  /* USER CODE END I2C2_MspInit 1 */
  }

}

/**
* @brief I2C MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hi2c: I2C handle pointer
* @retval None
*/
void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
{
  if(hi2c->Instance==I2C1)
  {
  /* USER CODE BEGIN I2C1_MspDeInit 0 */

  /* USER CODE END I2C1_MspDeInit 0 */
    /* Peripheral clock disable */
	  __HAL_RCC_I2C1_CLK_DISABLE();

	  /**I2C1 GPIO Configuration
	  PB8     ------> I2C1_SCL
	  PB9     ------> I2C1_SDA
	  */
	  HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8);

	  HAL_GPIO_DeInit(GPIOB, GPIO_PIN_9);

	  /* I2C1 DMA DeInit */
	  HAL_DMA_DeInit(hi2c->hdmarx);

	  /* I2C1 interrupt DeInit */
	  HAL_NVIC_DisableIRQ(I2C1_EV_IRQn);
	  /* USER CODE BEGIN I2C1_MspDeInit 1 */

	  /* USER CODE END I2C1_MspDeInit 1 */
  }
  else if(hi2c->Instance==I2C2)
  {
  /* USER CODE BEGIN I2C2_MspDeInit 0 */

  /* USER CODE END I2C2_MspDeInit 0 */
    /* Peripheral clock disable */
	  __HAL_RCC_I2C2_CLK_DISABLE();

	  /**I2C2 GPIO Configuration
	  PB10     ------> I2C2_SCL
	  PB11     ------> I2C2_SDA
	  */
	  HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10);

	  HAL_GPIO_DeInit(GPIOB, GPIO_PIN_11);

	  /* I2C2 DMA DeInit */
	  HAL_DMA_DeInit(hi2c->hdmatx);
	  /* USER CODE BEGIN I2C2_MspDeInit 1 */

	  /* USER CODE END I2C2_MspDeInit 1 */
  }

}

/**
* @brief TIM_Base MSP Initialization
* This function configures the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{
  if(htim_base->Instance==TIM1)
  {
  /* USER CODE BEGIN TIM1_MspInit 0 */

  /* USER CODE END TIM1_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_TIM1_CLK_ENABLE();
  /* USER CODE BEGIN TIM1_MspInit 1 */

  /* USER CODE END TIM1_MspInit 1 */
  }

}

void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(htim->Instance==TIM1)
  {
  /* USER CODE BEGIN TIM1_MspPostInit 0 */

  /* USER CODE END TIM1_MspPostInit 0 */

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**TIM1 GPIO Configuration
    PA8     ------> TIM1_CH1
    PA9     ------> TIM1_CH2
    */
    GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* USER CODE BEGIN TIM1_MspPostInit 1 */

  /* USER CODE END TIM1_MspPostInit 1 */
  }

}
/**
* @brief TIM_Base MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
{
  if(htim_base->Instance==TIM1)
  {
  /* USER CODE BEGIN TIM1_MspDeInit 0 */

  /* USER CODE END TIM1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_TIM1_CLK_DISABLE();
  /* USER CODE BEGIN TIM1_MspDeInit 1 */

  /* USER CODE END TIM1_MspDeInit 1 */
  }

}

/**
* @brief UART MSP Initialization
* This function configures the hardware resources used in this example
* @param huart: UART handle pointer
* @retval None
*/
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(huart->Instance==USART2)
  {
  /* USER CODE BEGIN USART2_MspInit 0 */

  /* USER CODE END USART2_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_USART2_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART2 GPIO Configuration
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX
    */
	  GPIO_InitStruct.Pin = GPIO_PIN_2;
	  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

	  GPIO_InitStruct.Pin = GPIO_PIN_3;
	  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	  GPIO_InitStruct.Pull = GPIO_NOPULL;
	  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

	  /* USART2 DMA Init */
	  /* USART2_RX Init */
	  hdma_usart2_rx.Instance = DMA1_Channel6;
	  hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
	  hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
	  hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE;
	  hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
	  hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
	  hdma_usart2_rx.Init.Mode = DMA_CIRCULAR;
	  hdma_usart2_rx.Init.Priority = DMA_PRIORITY_LOW;
	  if (HAL_DMA_Init(&hdma_usart2_rx) != HAL_OK) {
		  Error_Handler();
	  }

	  __HAL_LINKDMA(huart, hdmarx, hdma_usart2_rx);

	  /* USER CODE BEGIN USART2_MspInit 1 */
	  HAL_NVIC_SetPriority(USART2_IRQn, 1, 1);
	  HAL_NVIC_EnableIRQ(USART2_IRQn);
	  /* USER CODE END USART2_MspInit 1 */
  }

}

/**
* @brief UART MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param huart: UART handle pointer
* @retval None
*/
void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
{
  if(huart->Instance==USART2)
  {
  /* USER CODE BEGIN USART2_MspDeInit 0 */

	  /* USER CODE END USART2_MspDeInit 0 */
	  /* Peripheral clock disable */
	  __HAL_RCC_USART2_CLK_DISABLE();

	  /**USART2 GPIO Configuration
	  PA2     ------> USART2_TX
	  PA3     ------> USART2_RX
	  */
	  HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2 | GPIO_PIN_3);

	  /* USART2 DMA DeInit */
	  HAL_DMA_DeInit(huart->hdmarx);
	  /* USER CODE BEGIN USART2_MspDeInit 1 */
	  HAL_NVIC_DisableIRQ(USART2_IRQn);
	  /* USER CODE END USART2_MspDeInit 1 */
  }

}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
